import request from '@/axios'
import type { UserType } from './types'
interface RoleParams {
  roleType: string
}

export const loginApi = (data: UserType): Promise<IResponse<UserType>> => {
  const params = {
    ...data,
    password: data.password
  }
  return new Promise((resolve) => {
    resolve({
      code: 200,
      data:
        params.role == 'role1'
          ? ({
              username: '企业机构角色',
              token: '123456789012312'
            } as UserType)
          : ({
              username: '审核机构',
              token: '1234567890123'
            } as UserType)
    })
  })
  // request.get({ url: '/user/doLogin', params })
}

export const loginOutApi = (): Promise<IResponse> => {
  return new Promise((resolve) => {
    resolve({
      code: 200,
      data: {}
    })
  })
  // request.get({ url: '/user/loginOut' })
}

export const getUserListApi = ({ params }: AxiosConfig) => {
  return request.get<{
    code: string
    data: {
      list: UserType[]
      total: number
    }
  }>({ url: '/mock/user/list', params })
}

export const getTestRoleApi = (params: RoleParams): Promise<IResponse<string[]>> => {
  return request.get({ url: '/mock/role/list2', params })
}

const filterMenu = (menuList: any[], roleType: any[]) => {
  return menuList.filter((item) => {
    if (item.children && item.children.length > 0) {
      item.children = filterMenu(item.children || [], roleType)
    }
    if (roleType.includes('Admin')) return true
    if (item.permission && item.permission.length > 0) {
      return item.permission.some((p: any) => roleType.includes(p))
    }
    return true
  })
}
